{
 "metadata": {
  "name": "debug_demo.ipynb"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Debugging Data Processing After an Expensive Data Input Call"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Expensive Data Input and Output Preallocation"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Assume we have some expensive input operation that provides access to the data of interest."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data_in = np.random.rand(1e7) # Insert expensive loading operation here\n",
      "data_out = np.zeros_like(data_in) # Preallocate for outputs"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Data Processing"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "What if we now want to debug into this data processing loop without having to repeat the expensive input above?"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%connect_info"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "{\n",
        "  \"stdin_port\": 53282, \n",
        "  \"ip\": \"127.0.0.1\", \n",
        "  \"control_port\": 55070, \n",
        "  \"hb_port\": 35406, \n",
        "  \"key\": \"\", \n",
        "  \"shell_port\": 52364, \n",
        "  \"transport\": \"tcp\", \n",
        "  \"iopub_port\": 63202\n",
        "}\n",
        "\n",
        "Paste the above JSON into a file, and connect with:\n",
        "    $> ipython <app> --existing <file>\n",
        "or, if you are local, you can connect with just:\n",
        "    $> ipython <app> --existing kernel-32219.json \n",
        "or even just:\n",
        "    $> ipython <app> --existing \n",
        "if this is the most recent IPython session you have started.\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for idx in xrange(1, len(data_in)):\n",
      "    data_out[idx] = data_in[idx - 1] + data_in[idx]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 14
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 14
    }
   ],
   "metadata": {}
  }
 ]
}